<?php
header("Content-Type:text/html; charset=utf-8");
include_once('../inc/init.php');
include_once('../inc/cache.php');
if(!isAdmin()){
	exit("请登录");
}

$action=strFilter($_GET['action']);
switch($action){
	case "savelinks":
		try{
			$links_delid=$_POST['links_delid'];

			//DELETE
			if(is_array($links_delid)) {
				$delids=implode(",",$links_delid);
				$db->row_delete("links","id in ($delids)");
			}

			//UPDATE
			$links_ordernum=$_POST['links_ordernum'];
			$links_name=$_POST['links_name'];
			$links_url=$_POST['links_url'];
			$links_content=$_POST['links_content'];
			$links_logo=$_POST['links_logo'];

			if(is_array($links_name)) {
				foreach($links_name as $key=>$link_name){
					$linkobj['ordernum']=intval($links_ordernum[$key]);
					$linkobj['name']=$links_name[$key];
					$linkobj['url']=$links_url[$key];
					$linkobj['content']=$links_content[$key];
					$linkobj['logo']=$links_logo[$key];
					$db->row_update("links",$linkobj,"id={$key}");
				}
			}

			//INSERT
			$newlinks_ordernum=$_POST['newlinks_ordernum'];
			$newlinks_name=$_POST['newlinks_name'];
			$newlinks_url=$_POST['newlinks_url'];
			$newlinks_content=$_POST['newlinks_content'];
			$newlinks_logo=$_POST['newlinks_logo'];

			if(is_array($newlinks_name)) {
				foreach($newlinks_name as $key=>$link_name){
					$linkobj['ordernum']=intval($newlinks_ordernum[$key]);
					$linkobj['name']=$newlinks_name[$key];
					$linkobj['url']=$newlinks_url[$key];
					$linkobj['content']=$newlinks_content[$key];
					$linkobj['logo']=$newlinks_logo[$key];
					$db->row_insert("links",$linkobj);
				}
			}
			writeLinksCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "edit":
		$ad=$_POST['ad'];
		$id=numFilter($_GET['id']);
		$ad['starttime'] = empty($ad['starttime']) ? 0 : (strtotime($ad['starttime'])-$cache_settings['timeoffset']*3600);
		$ad['stoptime'] = empty($ad['stoptime']) ? 0 : (strtotime($ad['stoptime'])+24*3600-$cache_settings['timeoffset']*3600-1);
		if(substr($ad['targets'],0,4)=='all,'){
			$ad['targets']='all';
		}
		if(is_array($ad)){
			try{
				if($id==0){
					$ad['isuse']=1;
					$db->row_insert("ads",$ad);
				}else{
					$db->row_update("ads",$ad,"id={$id}");
				}
				writeAdsCache();
				echo("y");
			}catch(Exception $e){
				echo($e);
			}
		}

	break;

	case "saveadall":
		try{
			$delid=$_POST['delid'];
			$isuse=$_POST['isuse'];
			$ordernum=$_POST['ordernum'];

			//DELETE
			if(is_array($delid)) {
				$delids=implode(",",$delid);
				$db->row_delete("ads","id in ($delids)");
			}
			//UPDATE
			if(is_array($ordernum)) {
				foreach($ordernum as $id=>$ordern){
					$tmpad1['ordernum']=$ordern;
					$tmpad1['isuse']=numFilter($isuse[$id]);
					$db->row_update("ads",$tmpad1,"id={$id}");
				}
			}
			writeAdsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;


	case "postannounces":
		try{
			$anc=$_POST['anc'];
			$anc['starttime'] = empty($anc['starttime'])?0:strtotime($anc['starttime'])-$cache_settings['timeoffset']*3600;
			$anc['stoptime'] = empty($anc['stoptime'])?0:strtotime($anc['stoptime'])+24*3600-$cache_settings['timeoffset']*3600-1;
			$anc['posttime'] = time();
			$ancid=numFilter($_POST['ancid']);
			$anc['username'] = $lg['username'];
			$anc['userid'] = $lg['userid'];
			$anc['fid'] = 0;
			if(substr($anc['targets'],0,4)=='all,'){
				$anc['targets']='all';
			}

			if(numFilter($anc['type'])==1){
				$anc['content']='';
			}
			if(empty($ancid)){
				$db->row_insert("announces",$anc);
			}else{
				$db->row_update("announces",$anc,"id={$ancid}");
			}
			writeAnnouncesCache();
			echo("y");

		}catch(Exception $e){
			echo($e);
		}
	break;

	case "doannounces":
		$ids=$_POST['delid'];
		$ordernum=$_POST['ordernum'];

		if(!empty($ids) && is_array($ids)){
			$idstr = implode(",", $ids);
			$db->row_delete("announces","id in ($idstr)");
		}

		if(is_array($ordernum)){
			foreach($ordernum as $key=>$order){
				$anc['ordernum']=$order;
				$db->row_update("announces",$anc,"id={$key}");
			}
		}
		writeAnnouncesCache();
		echo("y");
	break;


	default:
		echo"No Such Action";
	break;
}

?>
